/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.shsc.controller;

import com.bionic.shsc.facade.TestFacade;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class TestController {

    private TestFacade testFacade;

    private TestFacade getTestFacade() {
        if (testFacade == null) {
            testFacade = new TestFacade();
        };
        return testFacade;
    }

    //@RequestMapping(method = RequestMethod.POST)
    @RequestMapping(value = "/test/{id}", method = RequestMethod.GET)
    ModelAndView testStart(@PathVariable(value = "id") int test_id, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TestFacade test_facade = getTestFacade();
        test_facade.getTest(test_id);
        return new ModelAndView("test", "testFacade", test_facade);

    }
  //@RequestMapping(method = RequestMethod.POST)
    @RequestMapping(value = "/test/{id}/next", method = RequestMethod.POST)
    ModelAndView NextQuestion(@PathVariable(value = "id") int test_id, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        TestFacade test_facade = getTestFacade();
        String[] checkedVals =  request.getParameterValues("answer");
        
       if  (test_facade.IsNextQuestion()){
           // get next question
           test_facade.getCurrentQuestion();
       }
       // else - will be save result and show problems
       
        return new ModelAndView("test", "testFacade", test_facade);

    }
   
}
